package com.dopies.bsdmall.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;

import com.dopies.BeeFramework.model.BaseModel;
import com.dopies.BeeFramework.model.BeeCallback;
import com.dopies.bsdmall.protocol.SESSION;
import com.dopies.bsdmall.protocol.STATUS;
import com.dopies.bsdmall.protocol.USER;
import com.external.androidquery.callback.AjaxStatus;

public class UserInfoModel extends BaseModel {

	public USER user;

	public static final int RANK_LEVEL_NORMAL = 0;
	public static final int RANK_LEVEL_VIP = 1;

	public UserInfoModel(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void getUserInfo() {

		String url = ProtocolConst.USERINFO;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {

				UserInfoModel.this.callback(url, jo, status);

				try {
					STATUS responseStatus = STATUS.fromJson(jo
							.optJSONObject("status"));

					if (responseStatus.succeed == 1) {
						user = USER.fromJson(jo.optJSONObject("data"));
						user.save();
						UserInfoModel.this.OnMessageResponse(url, jo, status);
					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		};

		SESSION session = SESSION.getInstance();

		JSONObject requestJsonObject = new JSONObject();

		Map<String, String> params = new HashMap<String, String>();
		try {
			requestJsonObject.put("session", session.toJson());
		} catch (JSONException e) {
			// TODO: handle exception
		}

		params.put("json", requestJsonObject.toString());

		cb.url(url).type(JSONObject.class).params(params);
		ProgressDialog pd = new ProgressDialog(mContext);
		pd.setMessage("请稍后...");
		aq.progress(pd).ajax(cb);

	}

}
